package com.zzyl.controller;

import cn.hutool.json.JSONUtil;
import com.aliyun.iot20180120.models.*;
import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.DeviceDto;
import com.zzyl.dto.DevicePageQueryDto;
import com.zzyl.service.DeviceService;
import com.zzyl.vo.DeviceVo;
import com.zzyl.vo.ProductVo;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

@RestController
@RequestMapping("/iot")
public class DeviceController extends BaseController{

    @Autowired
    private DeviceService deviceService;

    @PostMapping("/syncProductList")
    public ResponseResult pageQueryDevice() {
        deviceService.syncProductList();
        return success();
    }

    @GetMapping("/allProduct")
    public ResponseResult<List<ProductVo>> queryProductList(){
        List<ProductVo> productVoList = deviceService.queryProductList();
        return success(productVoList);
    }

    @PostMapping("/RegisterDevice")
    public ResponseResult registerDevice(@RequestBody DeviceDto deviceDto){
        deviceService.registerDevice(deviceDto);
        return success();
    }

    @GetMapping("/pageQueryDevice")
    public ResponseResult<PageResponse<DeviceVo>> PageDeviceList(DevicePageQueryDto devicePageQueryDto){
         PageResponse<DeviceVo> pageResponse = deviceService.PageDeviceList(devicePageQueryDto);
         return success(pageResponse);
    }

    @PostMapping("/QueryDeviceDetail")
    public ResponseResult<DeviceVo> queryDeviceById(@RequestBody QueryDeviceDetailRequest queryDeviceDetailRequest){
         DeviceVo deviceVo = deviceService.queryDeviceById(queryDeviceDetailRequest);
         return success(deviceVo);
    }

    @PostMapping("/QueryThingModelPublished")
    public ResponseResult<QueryThingModelPublishedResponseBody.QueryThingModelPublishedResponseBodyData> queryThingModelPublished(@RequestBody QueryThingModelPublishedRequest request) {
        return deviceService.queryThingModelPublished(request);
    }

    @PostMapping("/UpdateDevice")
    public ResponseResult UpdateDevice(@RequestBody DeviceDto deviceDto){
        deviceService.UpdateDevice(deviceDto);
        return success();
    }

    @DeleteMapping("/DeleteDevice")
    public ResponseResult deleteDevice(@RequestBody DeleteDeviceRequest request) {
        deviceService.deleteDevice(request);
        return ResponseResult.success();
    }

    @PostMapping("/QueryDevicePropertyStatus")
    @ApiOperation(value = "查询指定设备的物模型运行状态")
    public ResponseResult<QueryDevicePropertyStatusResponseBody.QueryDevicePropertyStatusResponseBodyData> queryDevicePropertyStatus(@RequestBody QueryDevicePropertyStatusRequest request) {
        return deviceService.queryDevicePropertyStatus(request);
    }
}
